package com.sky.happygos.present;

import android.util.Log;

import com.sky.happygos.event.CreateGoodsE;
import com.sky.happygos.event.GoodsE;
import com.sky.happygos.event.GoodsListE;
import com.sky.happygos.event.RegisterE;
import com.sky.happygos.model.Goods;
import com.sky.happygos.model.RestMessage;
import com.sky.happygos.network.Api;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static org.androidannotations.annotations.EBean.Scope.Singleton;


/**
 * Created by Jin on 2017/4/13.
 */
@EBean(scope = Singleton)
public class GoodsP {

    @Bean
    Api api;

    @Background
    public void getGoods(String shopid){
        Call<List<Goods>> call = api.getAuthService().goods(shopid);
        call.enqueue(new Callback<List<Goods>>() {
            @Override
            public void onResponse(Call<List<Goods>> call, Response<List<Goods>> response) {
                if(response.code() == 200){
                    List<Goods> goods = response.body();
                    EventBus.getDefault().post(new GoodsListE(goods,true));
                }else{
                    EventBus.getDefault().post(new GoodsListE(response));
                }
                Log.v("goods:===>",response.code()+"");
            }

            @Override
            public void onFailure(Call<List<Goods>> call, Throwable t) {
                EventBus.getDefault().post(new GoodsListE(null));
            }
        });
    }

    @Background
    public void getGoodsById(String goodsId){
        Call<Goods> call = api.getAuthService().goodsById(goodsId);
        call.enqueue(new Callback<Goods>() {
            @Override
            public void onResponse(Call<Goods> call, Response<Goods> response) {
                if(response.code() == 200){
                    Goods goods = response.body();
                    EventBus.getDefault().post(new GoodsE(goods,true));
                }else{
                    EventBus.getDefault().post(new GoodsE(response));
                }
                Log.v("goods:===>",response.code()+"");
            }

            @Override
            public void onFailure(Call<Goods> call, Throwable t) {
                EventBus.getDefault().post(new GoodsE(null));
            }
        });
    }
    @Background
    public void createGoods(String path,String shopId,Goods goods){
        File file = new File(path);
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        builder.addFormDataPart("name",goods.name);
        builder.addFormDataPart("info",goods.info);
        builder.addFormDataPart("shopid",shopId);
        builder.addFormDataPart("count",goods.count+"");
        builder.addFormDataPart("type",goods.type+"");
        builder.addFormDataPart("price",goods.price+"");
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("icon", file.getName(), imageBody);
        List<MultipartBody.Part> parts = builder.build().parts();

        Call<Goods> call = api.getAuthService().createGoods(parts);
        call.enqueue(new Callback<Goods>() {
            @Override
            public void onResponse(Call<Goods> call, Response<Goods> response) {

                if(response.code() == 200) {
                    Goods g = response.body();
                    EventBus.getDefault().post(new CreateGoodsE(g,true));
                }else{
                    EventBus.getDefault().post(new CreateGoodsE(response));
                }
            }

            @Override
            public void onFailure(Call<Goods> call, Throwable t) {
                EventBus.getDefault().post(new CreateGoodsE(null));
            }
        });
    }

    @Background
    public void updateGoods(String path,Goods goods){

        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        builder.addFormDataPart("id",goods.id);
        builder.addFormDataPart("name",goods.name);
        builder.addFormDataPart("info",goods.info);
        builder.addFormDataPart("count",goods.count+"");
        builder.addFormDataPart("type",goods.type+"");
        builder.addFormDataPart("price",goods.price+"");

        if(path != null) {
            File file = new File(path);
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("icon", file.getName(), imageBody);
        }

        List<MultipartBody.Part> parts = builder.build().parts();

        Call<Goods> call = api.getAuthService().updateGoods(parts);
        call.enqueue(new Callback<Goods>() {
            @Override
            public void onResponse(Call<Goods> call, Response<Goods> response) {

                if(response.code() == 200) {
                    Goods g = response.body();
                    EventBus.getDefault().post(new CreateGoodsE(g,true));
                }else{
                    EventBus.getDefault().post(new CreateGoodsE(response));
                }
            }

            @Override
            public void onFailure(Call<Goods> call, Throwable t) {
                EventBus.getDefault().post(new CreateGoodsE(null));
            }
        });
    }
}
